package com.goodedeeds.webapp.controller;

import com.goodedeeds.domain.IVendor;
import com.goodedeeds.domain.impl.db.DomainFactory;
import com.goodedeeds.domain.resource.Vendor;
import com.youtills.core.admin.ILookup;
import com.youtills.core.admin.LookupType;
import com.youtills.core.admin.db.dao.AdminDao;
import com.youtills.core.lang.StringUtil;
import com.youtills.resource.ResourceManager;
import net.sourceforge.stripes.action.DefaultHandler;
import net.sourceforge.stripes.action.RedirectResolution;
import net.sourceforge.stripes.action.Resolution;
import net.sourceforge.stripes.action.UrlBinding;

import java.util.List;

/**
 * Created by IntelliJ IDEA.
 * User: rchiluve
 * Date: 2/23/14
 * Time: 8:15 PM
 * To change this template use File | Settings | File Templates.
 */
@UrlBinding(VendorAction.URL)
public class VendorAction extends BaseDashboardAction {

    public static final String URL = "/vendor.action";

    private static final String VIEW = JSP_ROOT + "/vendor-view.jsp";
    private static final String EDIT_VIEW = JSP_ROOT + "/vendor-edit.jsp";
    private static final String LIST_VIEW = JSP_ROOT + "/vendor-list.jsp";

    private Long vendorId;

    private Vendor vendor;

    private String dashboardContent;

    private List<Vendor> vendors;

    private String keyword;

    @Override
    public String getDashboardContent() {
        return dashboardContent;
    }

    @Override
    public DashboardTab getCurrentTab() {
        return DashboardTab.NONE;
    }


    public Long getVendorId() {
        if(vendorId == null){
            vendorId = StringUtil.getNumber(getRequestParameter("vendorId"), 0).longValue();
        }
        return vendorId;
    }

    public void setVendorId(Long vendorId) {
        this.vendorId = vendorId;
    }

    public Vendor getVendor() {
        if(vendor == null){
//            vendor = DomainFactory.getVendorDao().getVendor(getAuthorization(), getVendorId());
            if(getVendorId() < 1) {
                vendor = ResourceManager.instance.newResource(getActionContext(), Vendor.class);
            } else {
                vendor = ResourceManager.instance.getResource(getActionContext(), Vendor.class, getVendorId());
            }
        }
        return vendor;
    }

    @DefaultHandler
    public Resolution list(){
        dashboardContent = LIST_VIEW;
        return showPage();
    }

    public Resolution search(){
        return list();
    }

    public List<Vendor> getVendors(){
        if(vendors == null){
            if(!StringUtil.isEmpty(getKeyword())){
//                vendors = DomainFactory.getVendorDao().searchVendors(getAuthorization(), getKeyword());
            }else {
//                vendors = DomainFactory.getVendorDao().getAllVendors(getAuthorization());
                vendors = ResourceManager.instance.getResources(getActionContext(), Vendor.class, null);
            }
        }
        return vendors;
    }

    public Resolution save(){
//        DomainFactory.getVendorDao().saveVendor(getAuthorization(), vendor);
        ResourceManager.instance.saveResource(getActionContext(), vendor);
        vendorId = vendor.getVendorId();
        RedirectResolution res = new RedirectResolution(VendorAction.class, "view");
        res.addParameter("vendorId", vendorId);
        return res;
    }

    public Resolution edit(){
        dashboardContent = EDIT_VIEW;
        return showPage();
    }
    public Resolution view(){
        dashboardContent = VIEW;
        return showPage();
    }

    private List<ILookup> vendorStatuses;
    public List<ILookup> getVendorStatuses(){
        if(vendorStatuses == null){
            vendorStatuses = AdminDao.getInstance().getLookups(LookupType.getByType(7L));
        }
        return vendorStatuses;
    }

    private List<ILookup> vendorCategories;
    public List<ILookup> getVendorCategories(){
        if(vendorCategories == null){
            vendorCategories = AdminDao.getInstance().getLookups(LookupType.getByType(10L));
        }
        return vendorCategories;
    }

    public String getKeyword() {
        if(keyword == null){
            keyword = getRequestParameter("keyword");
        }
        return keyword;
    }

    public void setKeyword(String keyword) {
        this.keyword = keyword;
    }
}
